package net.sansi.v3correctserver.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sansi.v3correctserver.constenum.AppHelper;
import net.sansi.v3correctserver.dto.R;
import net.sansi.v3correctserver.dto.correctresult.CorrectResultDto;
import net.sansi.v3correctserver.exception.ServiceException;
import net.sansi.v3correctserver.service.correctresult.CorrectResultService;
import net.sansi.v3correctserver.task.SolidifyFactorTask;
import net.sansi.v3correctserver.task.UploadFactorTask;
import net.sansi.v3correctserver.task.V3Task;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * Created by Fang
 * create time : 2024/11/11
 * IDE : IntelliJ IDEA
 * <p>
 * 矫正结果相关接口
 */

@RestController
@Api(tags = "矫正结果相关接口")
@RequestMapping("/result")
@Validated
public class CorrectResultController {

    @Resource
    private CorrectResultService correctResultService;

    @ApiOperation("系数上传（开始系数上传任务）, 返回任务id")
    @PostMapping("/start-upload-factor")
    public R<String> startUploadFactor(@RequestBody @Validated CorrectResultDto.StartUploadFactorReq form) {
        return R.data(correctResultService.startUploadFactor(form));
    }

    @ApiOperation("生成矫正系数")
    @PostMapping("/gen-factor")
    public R<String> genFactor(@RequestBody @Validated CorrectResultDto.GenFactorReq form) {
        return R.data(correctResultService.genFactor(form));
    }

    @ApiOperation("查询系数上传进度")
    @GetMapping("/query/upload-factor-progress")
    public R<UploadFactorTask.Progress> queryUploadFactorProgress(String taskId) {
        V3Task<?, ?> task = AppHelper.getTaskManager().get(taskId);
        if (task == null) throw new ServiceException("任务不存在 {}", taskId);
        return R.data((UploadFactorTask.Progress) task.getProgress());
    }

    @ApiOperation("系数固化 (开始系数固化任务), 返回任务id")
    @PostMapping("/start-solidify-factor")
    public R<String> startSolidifyFactor(@RequestBody @Validated CorrectResultDto.StartSolidifyFactorReq form) {
        return R.data(correctResultService.startSolidifyFactor(form));
    }

    @ApiOperation("查询系数固化进度")
    @GetMapping("/query/solidify-factor-progress")
    public R<SolidifyFactorTask.Progress> querySolidifyFactorProgress(String taskId) {
        V3Task<?, ?> task = AppHelper.getTaskManager().get(taskId);
        if (task == null) throw new ServiceException("任务不存在 {}", taskId);
        return R.data((SolidifyFactorTask.Progress) task.getProgress());
    }
}
